package com.siwei.mes.service.laboratory.impl;

import com.alibaba.fastjson.JSON;
import com.siwei.mes.entity.laboratory.ContractData;
import com.siwei.mes.entity.sync.erp.ErpCustomer;
import com.siwei.mes.entity.sync.erp.ErpProject;
import com.siwei.mes.enums.SystemParamEnum;
import com.siwei.mes.mapper.laboratory.ContractInfoMapper;
import com.siwei.mes.mapper.laboratory.KeyPropertyMapper;
import com.siwei.mes.mapper.syncErp.ErpCustomerMapper;
import com.siwei.mes.mapper.syncErp.ErpProjectMapper;
import com.siwei.mes.service.laboratory.ContractInfoService;
import com.siwei.mes.service.system.SystemParamService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import static com.siwei.mes.common.exception.handler.MessageHandler.assertTrue;

/**
 * 协会工程（Contract_Info）
 *
 * @Description: 协会工程（Contract_Info）
 * @Version: 1.0
 * @author: mlchen
 * @date: 2024/7/26 11:29
 */
@Service
public class ContractInfoServiceImpl implements ContractInfoService {
    private static final Logger log = LoggerFactory.getLogger("laboratory");
    @Resource
    private ContractInfoMapper contractInfoMapper;
    @Resource
    private ErpCustomerMapper erpCustomerMapper;
    @Resource
    private KeyPropertyMapper keyPropertyMapper;
    @Resource
    private ErpProjectMapper erpProjectMapper;
    @Resource
    private SystemParamService systemParamService;

    @Override
    public String saveOrUpdate(ErpProject erpProject, Boolean isNotify, Boolean isMock) {
        log.info("正在处理【协会软件】新增或修改【协会工程】信息入参：{}", JSON.toJSONString(erpProject));

        /**判断是否开启推送混凝土任务单到协会开关*/
        String systemParamValue = systemParamService.selectBySystemCode(SystemParamEnum.PUSH_PRODUCE_HNT, "0");
        /**如果这里是模拟同步 不管是什么直接默认1*/
        if (isMock) {
            systemParamValue = "1";
        }
        if (StringUtils.isBlank(systemParamValue) || "0".equals(systemParamValue)) {
            log.info("系统参数：【{}】暂未开启推送混凝土任务单到协会软件，不推送协会工程信息！", SystemParamEnum.PUSH_PRODUCE_HNT);
            return null;
        }
        if (erpProject == null) {
            return null;
        }
        ContractData contractData = null;
        try {
            Long erpCustomerId = erpProject.getErpCustomerId();
            ErpCustomer erpCustomer = erpCustomerMapper.loadById(erpCustomerId);
            assertTrue(erpCustomer == null, "品控客户信息不存在");
            String erpCustomerLaboratoryId = erpCustomer.getLaboratoryId();//客户协会id
            assertTrue(StringUtils.isBlank(erpCustomerLaboratoryId), "品控客户信息未上传到协会，请先上传");

            /**根据客户协会id和合同编号和工程名称查询协会数据*/
            //contractData = contractInfoMapper.selectIsExist(erpCustomerLaboratoryId, erpProject.getContractNo(), erpProject.getProjectName());
            /**2024-09-20 11:00:51  这里根据客户id 和合同编号来查询唯一的  因为一个合同编号只能有一个工程*/
            contractData = contractInfoMapper.selectIsExist(erpCustomerLaboratoryId, erpProject.getContractNo(), null);

            if (contractData == null) {
                contractData = ContractData.payloadInfo(erpProject, null, erpCustomerLaboratoryId);
                String newId = keyPropertyMapper.getNewId();
                contractData.setContractId(newId);
                /**生成uploadFlag*/
                //String uploadFlag = UploadFlagUtils.CtoGB_Num("0000000000", "z", new Date(), null);
                /**这里写死*/
                contractData.setUploadFlag("*!@@#@#$@#$@#$@#$@#$@#$@#$@#$@#$@#$%*!@@#$*!@$@%?#&@#$$@%?#&@#$*!@");
                contractInfoMapper.insert(contractData);
            } else {
                String contractId = contractData.getContractId();//协会合同id
                //String uploadFlag = contractData.getUploadFlag();
                contractData = ContractData.payloadInfo(erpProject, contractId, erpCustomerLaboratoryId);
                contractInfoMapper.updateByPrimaryKey(contractData);
            }
            erpProjectMapper.updateIsUploadInfo(erpProject.getId(), contractData.getContractId(), 1, null);
            log.info("处理【协会软件】新增或修改【协会工程】成功！");
            return contractData.getContractId();
        } catch (Exception e) {
            log.info("新增或修改协会工程信息和合同信息失败，入参：{}", JSON.toJSONString(contractData));
            log.info("新增或修改协会工程信息和合同信息失败，错误信息：{}", e.getMessage(), e);
            erpProjectMapper.updateIsUploadInfo(erpProject.getId(), null, 0, e.getMessage());
            assertTrue(isNotify, "上传工程信息与合同信息失败，失败原因：【" + e.getMessage() + "】");
            return null;
        }
    }
}
